<?php

namespace app\data\controller\api\auth;

use app\data\controller\api\Auth;
use app\data\model\ShopGoodsCart;
use app\data\model\ShopGoods;

/**
 * 用户购物车
 * Class Cart
 * @package app\data\controller\api\auth
 */
class Cart extends Auth
{
    
    public function index(){
        $CartObj = ShopGoodsCart::order('id','desc')->select();
        //对象转数组，判断是否为空
        $CartArr = json_decode($CartObj);
        if(count($CartArr) == 0){
            $this->error('用户购物车列表为空',$CartObj);
        }
        $this->success('获取用户购物车成功',$CartObj);
    }
    
    public function add()
    {  
        $data= $this->_vali([
                'uid.value'             => $this->uuid,
                'goods_id.require'      => '商品id不能为空！',
                'goods_code.require'    => '商品编号不能为空！',
                'goods_price.require'   => '商品价格不能为空！',
                'goods_name.require'    => '商品名称不能为空！',
                'goods_cover.require'   => '商品图片不能为空！',
                'goods_spec.require'    => '商品规格不能为空！',
                'goods_spec_name.require'  => '商品规格名称不能为空！',
                'goods_number.require'  => '商品数量不能为空！',
                'store_id.require'      => '店铺id不能为空！',
                'store_name.require'    => '店铺名称不能为空！',
                'goods_max_num.require' => '最大值不能为空',
                'stock_total.require'   => '库存不能为空'
            ]);
        
        //检查商品是否存在
        $goodsID = ShopGoods::where('id',$data["goods_id"])->find();
        
        if (empty($goodsID["id"])) {
            $this->error($goodsID,"商品id不存在");
        }
        //是否存在同一件商品,是：增加数量，否：添加为新商品
        $select = ShopGoodsCart::where('goods_id',$data["goods_id"])->find();
        if($select["goods_id"] == $data["goods_id"]){
            if($select["goods_number"]+$data["goods_number"]>$goodsID["stock_total"]){
                $CartNumber = $goodsID["stock_total"];
            }else {
                $CartNumber = $select["goods_number"]+$data["goods_number"];
            }
            $updataCart = ShopGoodsCart::where('goods_id',$data["goods_id"])->update(['goods_number'=>$CartNumber]);
            $this->success("商品+1",$updataCart);
        }
        
        $ShopGoodsCart = new ShopGoodsCart();
        $query = $ShopGoodsCart->save($data);
        $this->success("加入购物车",$query);
    }
    //修改商品数量
    public function changeNumber(){
        $number = $this->request->param('goods_number','');
        $id = $this->request->param('cartID','');
        if($number == '' || $id == ''){
            $this->error('数量或者cartID为空',$id);
        }
        $updateNumber = ShopGoodsCart::where('id',$id)->update(['goods_number'=>$number]);
        $this->success('数量修改成功',$number);
    }
    
    public function Del(){
        $Cartid = $this->request->param('id','');
        if(empty($Cartid)){
            $this->error('CartId为空',$Cartid);
        }
        $del = ShopGoodsCart::where('id',$Cartid)->delete();
        $this->success('删除成功！ ',$del);
    }
}








































